package buaa.leetcode.utilBeans;

public class ConvertSortedArraytoBinarySearchTree {
	public static TreeNode sortedArrayToBST(int[] num) {
        if(num.length == 0){
        	return null;
        }
        if(num.length == 1){
        	return new TreeNode(num[0]);
        }
        int len = num.length;
        int left = 0;
        int right = len - 1;
        return sortedArrayToBST(num,left,right);
    }
	public static TreeNode sortedArrayToBST(int[] num,int left,int right) {
		  if(left == right){
			  return new TreeNode(num[left]);
		  }
		  int center = (left + right)/2;
	      TreeNode root = new TreeNode(num[center]);
	      if(center > left){
	    	  root.left = sortedArrayToBST(num, left, center-1);
	      }
	      if(center < right){
	    	  root.right = sortedArrayToBST(num, center+1, right);
	      }
		  return root;
	}
}	
